package com.example.drawpicture;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PathView extends View {
    //画笔
    protected Paint paint;
    //画笔
    protected Paint paintAxes2;
    //心电图折线
    protected Path path;
    //自身的大小
    private int width, height;
    int tmpX;
    //折现的颜色
    private int lineColor = Color.parseColor("#000000");

    public float getPositionX() {
        return positionX;
    }

    public void setPositionX(float positionX) {
        this.positionX = positionX;
    }

    public float getPositionY() {
        return positionY;
    }

    public void setPositionY(float positionY) {
        this.positionY = positionY;
    }

    private float positionX = 10,positionY = 700;

    private String[] xnums={"0","1","2","3","4","5","6","7","8","9","10"};
    private String[] ynums={"0","1024"};
    private CharSequence charSequence;
    private static final int Max_Y = 1080;
    private static final int Max_X = 2111;
    private static final int  startX = 20;

    private static int  LINE_NUM = 100;
    private int[] dataForShow={10,66,225,444,81,5,66,266,10,98,322,888};

    private List<Integer> list1 = new ArrayList<>();

    public PathView(Context context) {
        this(context, null);
    }

    public PathView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PathView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        path = new Path();

        paintAxes2 = new Paint();
    }

    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private void drawPath(Canvas canvas) {

        int jiange = 50;
        List<Integer> list2 = list1;

        tmpX = 0;
        paint.reset();
        //设置画笔style
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(lineColor);
        paint.setStrokeWidth(5);

        //设置画笔style
        paintAxes2.setStyle(Paint.Style.STROKE);
        paintAxes2.setColor(lineColor);
        paintAxes2.setStrokeWidth(1);

        path.reset();
        path.moveTo(positionX, 500);

        //调节好每个波的X轴距离，尽量和滑动的速度保持一致
        for (int i = 0; i < list2.size(); i++) {
            tmpX += jiange;
            int high = list2.get(i) / 2;
            path.lineTo(positionX + tmpX, 500 - high);

            String dateStr = dateformat.format(System.currentTimeMillis());
            canvas.drawText(dateStr.substring(dateStr.length()-4),positionX + tmpX, 500,paintAxes2);
        }
        canvas.drawPath(path, paint);

         //    画刻度
//        for (int i = 0; i < 2; i++) {
            canvas.drawText("0",20,500,paintAxes2);
        canvas.drawText("512",20,250,paintAxes2);
            canvas.drawText("1024",20,10,paintAxes2);
//        }

        //画表格
        for (int i = 1; i <= LINE_NUM + list2.size(); i++) {
            //画横线     Max_Y = 580   Max_X = 1000  startX = 20
            canvas.drawLine(startX,Max_Y/LINE_NUM*i,Max_X,Max_Y/LINE_NUM*i,paintAxes2);
            canvas.drawLine(startX,Max_Y,startX,startX,paintAxes2);
            canvas.drawLine((Max_X-startX)/LINE_NUM*i,Max_Y,(Max_X-startX)/LINE_NUM*i,startX,paintAxes2);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        width = w;
        height = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawPath(canvas);
        if (j == 0) {
            if (list1.size() > 100 && list1.size() < 599) {
                scrollBy(1, 0);
            } else if (list1.size() >= 599) {
                scrollTo(0, 0);
            }
        } else if (j == -1) {
            scrollBy(10, 0);
        } else if (j == -2) {
            scrollBy(0, 0);
        }
    }

    private int i = 0;
    private int j = 0;

    public void setData(int data) {
        //定期删除历史数据，防止图片过长导致崩溃
        if (list1.size() % 3600 == 0) {
//            list.clear();
//            scrollTo(0, 0);

//            StringBuffer stringBuffer = new StringBuffer();
//            HttpHelper.sendPostMessage(stringBuffer);
        } else if (list1.size() >= 36000 * 10) {
            list1.clear();
            scrollTo(0, 0);
        }
        list1.add(dataForShow[i]);
        i = i + 1;
        if (i >= dataForShow.length) {
            i = 0;
        }
        postInvalidate();
    }

    public void drawLine(){
        j = 2;
        postInvalidate();
    }

    public void setNum(float num){
        positionX = 10;
        positionY = height * num;
    }

    public void pollData() {
        j = -1;
        scrollTo(LINE_NUM, 0);
        LINE_NUM += 100;
    }

    public void pullData() {
        j = -2;
        scrollTo(0, 0);
        LINE_NUM = 0;
    }

}
